package com.coding.controller;


import com.coding.base.pojo.BaseModel;
import com.coding.pojo.OssImages;
import com.coding.service.OssImagesService;
import com.coding.service.UserService;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@Controller
@ResponseBody
@RequestMapping("user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    OssImagesService ossImagesService;

    @ApiOperation(value = "登录")
    @ResponseBody
    @RequestMapping(value = "/login", method = {RequestMethod.POST})
    public BaseModel userLogin(@ApiParam("账号") @RequestParam(value = "account") String account, @ApiParam("密码") @RequestParam(value = "pwd") String pwd) throws Exception {
        BaseModel model = new BaseModel();
        userService.userLogin(account, pwd, model);
        return model;
    }

    @ApiOperation(value = "验证码登录")
    @ResponseBody
    @RequestMapping(value = "/loginWithCheck", method = {RequestMethod.POST})
    public BaseModel userLogin(@ApiParam("账号") @RequestParam(value = "account") String account) throws Exception {
        BaseModel model = new BaseModel();
        userService.userloginwithcheck(account, model);
        return model;
    }

    @ApiOperation(value = "注册")
    @ResponseBody
    @RequestMapping(value = "/register", method = {RequestMethod.POST})
    public BaseModel userRegister(@ApiParam("账号") @RequestParam("account") String account,
                                  @ApiParam("加密后的密码") @RequestParam("pwd") String pwd) throws Exception {
        BaseModel model = new BaseModel();
        userService.userRegister(account, pwd, model);
        return model;
    }

    @ApiOperation(value = "通过账户获得用户信息")
    @ResponseBody
    @RequestMapping(value = "/getUser", method = {RequestMethod.GET})
    public BaseModel getuser(@ApiParam("账号") @RequestParam("account") String account) throws Exception {
        BaseModel model = new BaseModel();
        userService.getuserbyacount(account, model);
        return model;
    }
    @ApiOperation(value = "通过id获得用户信息")
    @ResponseBody
    @RequestMapping(value = "/getUserById", method = {RequestMethod.GET})
    public BaseModel getUserById(@ApiParam("user_id") @RequestParam("user_id") int user_id) throws Exception {
        BaseModel model = new BaseModel();
        userService.selectById(user_id,model);
        return model;
    }
    @ApiOperation(value = "找回密码")
    @ResponseBody
    @RequestMapping(value = "/getBackPassword", method = {RequestMethod.POST})
    public BaseModel userRegister(@ApiParam("账号") @RequestParam("account") String account) throws Exception {
        BaseModel model = new BaseModel();
        userService.usergetpassword(account, model);
        return model;
    }

    @ApiOperation(value = "更改用户名")
    @ResponseBody
    @RequestMapping(value = "/updateUsername", method = {RequestMethod.PUT})
    public BaseModel changeuername(@ApiParam("用户名") @RequestParam("username") String username,
                                   @ApiParam("账号") @RequestParam("account") String account
    ) throws Exception {
        BaseModel model = new BaseModel();
        userService.changeusername(username, account, model);
        return model;
    }
    @ApiOperation(value = "更改用户密码")
    @ResponseBody
    @RequestMapping(value = "/updateUserpwd", method = {RequestMethod.PUT})
    public BaseModel changeuerpwd(@ApiParam("密码") @RequestParam("pwd") String pwd,
                                   @ApiParam("账号") @RequestParam("account") String account
    ) throws Exception {
        BaseModel model = new BaseModel();
        userService.changeuserpwd(pwd,account,model);
        return model;
    }

    @ApiOperation(value = "获得用户名头像")
    @ResponseBody
    @RequestMapping(value = "/getuserimage/{account}", method = {RequestMethod.GET})
    public BaseModel getuserimage(@ApiParam("账号") @PathVariable("account") String account) throws Exception {
        BaseModel model = new BaseModel();
        userService.getUserimage(account, model);
        return model;
    }

    @ApiOperation(value = "更改用户头像")
    @RequestMapping(value = "/updateuserimage", method = RequestMethod.POST, consumes = "multipart/*", headers = "content-type=multipart/form-data")
    @ResponseBody
    public BaseModel updateuserimage(@ApiParam("账户") @RequestParam("account") String account, HttpServletRequest request, MultipartFile file) {
        BaseModel baseModel = new BaseModel();
        try {
            ossImagesService.setOssimage(file, baseModel);
            if (baseModel.getResultCode() == 1) {
                OssImages ossImages = (OssImages) baseModel.getData();
                userService.changeUserimage(ossImages.getImage_id(), account, baseModel);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return baseModel;
    }

    @ApiOperation(value = "增加用户积分")
    @RequestMapping(value = "/addUserPoints", method = {RequestMethod.POST})
    @ResponseBody
    public BaseModel addUserPoints(@ApiParam("用户id") @RequestParam("user_id") int user_id,@ApiParam("用户获得积分") @RequestParam("points") int points) {
        BaseModel baseModel = new BaseModel();
        try {
            userService.addUserPoints(user_id,points,baseModel);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return baseModel;
    }

    @ApiOperation(value = "获得用户积分排名")
    @RequestMapping(value = "/getOrderList", method = {RequestMethod.GET})
    @ResponseBody
    public BaseModel addUserPoints() {
        BaseModel baseModel = new BaseModel();
        try {
            userService.getOrderPoints(baseModel);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return baseModel;
    }

    @ApiOperation(value = "获得用户列表")
    @RequestMapping(value = "/getUserList", method = {RequestMethod.GET})
    @ResponseBody
    public BaseModel getUserList() {
        BaseModel baseModel = new BaseModel();
        try {
            userService.getUserList(baseModel);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return baseModel;
    }
}
